package org.kevinth.mail;

import java.util.regex.Pattern;

/**
 * @author <a href="mailto:tulkas.web@gmail.com">Tulkas</a>
 *
 */
public class EmailChecker {
	public static Pattern ptn = Pattern
			.compile("\\w([-.\\w]*\\w)?@\\w([-.\\w]*\\w)?\\.\\w+");
	public static Pattern ptnQuote = Pattern
			.compile("['\"<]\\w([-.\\w]*\\w)?@\\w([-.\\w]*\\w)?\\.\\w+['\">]");

	public static boolean checkEmail(String email) {
		return ptn.matcher(email).matches();
	}

	public static String unQuote(String email) {
		if (email == null || "".equals(email))
			return null;
		if (ptnQuote.matcher(email).matches()) {
			return email.substring(1, email.length() - 1);
		} else
			return null;
	}

	public static String getDomain(String email) {
		int idx = email.indexOf('@');
		if (idx != -1)
			return email.substring(idx + 1).toLowerCase();
		else
			return null;
	}

	public static String getUser(String email) {
		int idx = email.indexOf('@');
		if (idx != -1)
			return email.substring(0, idx);
		else
			return null;
	}

	public static void main(String[] args) {
		System.out.println(getUser("kk105678.adfasfd@yahoo.com.tw"));
	}
}
